//
//  MYHAleartView.m
//  YiBaoStore
//
//  Created by 毛云河 on 2018/11/26.
//  Copyright © 2018 小河. All rights reserved.
//

#import "MYHAleartView.h"

@interface MYHAleartView ()

@property (nonatomic, assign) CGFloat shopCostPrice;//店铺成本价
@property (nonatomic, assign) CGFloat marketPrice;//市场划价线
@property (nonatomic, assign) CGFloat price;//店铺售卖价
@property (nonatomic, assign) CGFloat f_newPrice;//改价后的价格
@property (nonatomic, strong) UITextField *changPriceTF;
@property (nonatomic, strong) UILabel *earningsLabel;//收益

@property (nonatomic, assign) NSInteger stock;//库存数
@property (nonatomic, strong) UITextField *stockTF;//库存
@property (nonatomic, strong) UITextField *promotionPriceTF;//活动价

@end

@implementation MYHAleartView

- (id)initMecommendAlertViewWithMessage:(NSString *)message
                                fuText:(NSString *)fuText
                           sureBtnTitle:(NSString *)sure
                         cancleBtnTitle:(NSString *)cancle
{
    self = [super init];
    if (self)
    {
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        [self createRecommendViewWithMessage:message fuText:fuText sureBtnTitle:sure cancleBtnTitle:cancle];
    }
    
    return self;
}

- (void)createRecommendViewWithMessage:(NSString *)message
                                fuText:(NSString *)fuText
                          sureBtnTitle:(NSString *)sure
                        cancleBtnTitle:(NSString *)cancle
{
    UIView *bgView = [[UIView alloc] init];
    [self addSubview:bgView];
    bgView.center = self.center;
    bgView.bounds = CGRectMake(0, 0, 210, 150);
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 5;
    bgView.layer.masksToBounds = YES;
    
    UIImageView *closeIcon = [[UIImageView alloc] init];
    [bgView addSubview:closeIcon];
    closeIcon.frame = CGRectMake(bgView.width-10-16, 10, 16, 16);
    closeIcon.image = [UIImage imageNamed:@"closeIcon"];
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [bgView addSubview:closeBtn];
    closeBtn.frame = CGRectMake(bgView.width-40, 0, 40, 40);
    [closeBtn addTarget:self action:@selector(closeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *messageLabel = [[UILabel alloc] init];
    [bgView addSubview:messageLabel];
    messageLabel.frame = CGRectMake((bgView.width-140)/2, 56, 140, 14);
    messageLabel.text = message;
    messageLabel.textAlignment = 1;
    messageLabel.textColor = BlackTextColor;
    messageLabel.font = FontWithSize(14);
    
    UILabel *fuLabel = [[UILabel alloc] init];
    [bgView addSubview:fuLabel];
    fuLabel.frame = CGRectMake(0, messageLabel.bottom+12, bgView.width, 12);
    fuLabel.font = FontWithSize(12);
    fuLabel.textColor = RGBACOLOR(194, 194, 194, 1);
    fuLabel.text = fuText;
    fuLabel.textAlignment = 1;
    
    UIView *lineView = [[UIView alloc] init];
    [bgView addSubview:lineView];
    lineView.frame = CGRectMake(0, 115, bgView.width, 1);
    lineView.backgroundColor = RGBACOLOR(238, 238, 238, 1);
    
    
    UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [bgView addSubview:cancleBtn];
    cancleBtn.frame = CGRectMake(0, lineView.bottom, 105, 34);
    [cancleBtn addTarget:self action:@selector(closeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [cancleBtn setTitle:cancle forState:UIControlStateNormal];
    [cancleBtn setTitleColor:RGBACOLOR(66, 66, 66, 1) forState:UIControlStateNormal];
    
    UIView *verLineView = [[UIView alloc] init];
    [bgView addSubview:verLineView];
    verLineView.backgroundColor = RGBACOLOR(238, 238, 238, 1);
    verLineView.frame = CGRectMake(cancleBtn.right, lineView.bottom+10, 1, 14);
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [bgView addSubview:sureBtn];
    sureBtn.frame = CGRectMake(verLineView.right, lineView.bottom, 105, 34);
    [sureBtn addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [sureBtn setTitle:sure forState:UIControlStateNormal];
    [sureBtn setTitleColor:RGBACOLOR(66, 66, 66, 1) forState:UIControlStateNormal];
}

#pragma mark - 关闭弹窗
- (void)closeBtnClick
{
    [self endEditing:YES];
    [self.stockTF resignFirstResponder];
    [self.promotionPriceTF resignFirstResponder];
    [self.changPriceTF resignFirstResponder];
    [self removeFromSuperview];
}

#pragma mark - 确认按钮点击事件
- (void)sureBtnClick
{
    if (self.sureBlock)
    {
        self.sureBlock(@"1");
    }
    [self removeFromSuperview];
}

/**********************************/

- (id)initChangPriceWithModel:(MainModel *)model
{
    self = [super init];
    if (self)
    {
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        [self createChangePriceViewWithModel:model];
    }
    
    return self;
}

- (void)createChangePriceViewWithModel:(MainModel *)model
{
    self.shopCostPrice = [model.shopCostPrice floatValue];//店铺成本价
    self.marketPrice = [model.marketPrice floatValue];//市场划价线
    self.price = [model.price floatValue];//店铺售卖价
    
    UIView *bgView = [[UIView alloc] init];
    [self addSubview:bgView];
    bgView.center = self.center;
    bgView.bounds = CGRectMake(0, 0, 275, 300);
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 5;
    bgView.layer.masksToBounds = YES;
    
    UIImageView *closeIcon = [[UIImageView alloc] init];
    [bgView addSubview:closeIcon];
    closeIcon.frame = CGRectMake(bgView.width-10-16, 10, 16, 16);
    closeIcon.image = [UIImage imageNamed:@"closeIcon"];
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [bgView addSubview:closeBtn];
    closeBtn.frame = CGRectMake(bgView.width-40, 0, 40, 40);
    [closeBtn addTarget:self action:@selector(closeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    [bgView addSubview:titleLabel];
    titleLabel.frame = CGRectMake((bgView.width-70)/2, 20, 70, 14);
    titleLabel.textColor = BlackTextColor;
    titleLabel.textAlignment = 1;
    titleLabel.font = FontWithSize(14);
    titleLabel.text = @"当前销售价";
    
    UILabel *priceLabel = [[UILabel alloc] init];
    [bgView addSubview:priceLabel];
    priceLabel.frame = CGRectMake((bgView.width-150)/2, titleLabel.bottom+13, 150, 18);
    priceLabel.textColor = BlackTextColor;
    priceLabel.textAlignment = 1;
    priceLabel.font = FontSemiboldSize(18);
    priceLabel.text = [NSString stringWithFormat:@"¥%0.2f",[model.price floatValue]];
    
    UILabel *showPriceLabel = [[UILabel alloc] init];
    [bgView addSubview:showPriceLabel];
    showPriceLabel.frame = CGRectMake((bgView.width-120)/2, priceLabel.bottom+30, 120, 24);
    showPriceLabel.textColor = BlackTextColor;
    showPriceLabel.textAlignment = 1;
    showPriceLabel.font = FontSemiboldSize(24);
    showPriceLabel.text = @"店主销售价";
    
    self.changPriceTF = [[UITextField alloc]init];
    [bgView addSubview:self.changPriceTF];
    self.changPriceTF.frame = CGRectMake(20, showPriceLabel.bottom+10, bgView.width-40, 40);
    self.changPriceTF.backgroundColor = RGBACOLOR(244, 244, 244, 1);
    self.changPriceTF.textAlignment = 1;
    self.changPriceTF.delegate = self;
    self.changPriceTF.keyboardType = UIKeyboardTypeDecimalPad;
    
    UILabel *priceAreaLabel = [[UILabel alloc] init];
    [bgView addSubview:priceAreaLabel];
    priceAreaLabel.frame = CGRectMake(30, self.changPriceTF.bottom+10, 130, 14);
    priceAreaLabel.textColor = RGBACOLOR(211, 211, 211, 1);
    priceAreaLabel.textAlignment = 0;
    priceAreaLabel.font = FontWithSize(14);
    priceAreaLabel.text = [NSString stringWithFormat:@"改价范围¥%0.2f-¥%0.2f",self.shopCostPrice,self.marketPrice];
    [priceAreaLabel sizeToFit];
    
    self.earningsLabel = [[UILabel alloc] init];
    [bgView addSubview:self.earningsLabel];
    self.earningsLabel.frame = CGRectMake(0, priceAreaLabel.bottom+10, bgView.width, 18);
    self.earningsLabel.textColor = BlackTextColor;
    self.earningsLabel.textAlignment = 1;
    self.earningsLabel.font = FontSemiboldSize(18);
    self.earningsLabel.text = [NSString stringWithFormat:@"收益：¥%0.2f",(self.price-self.shopCostPrice)];
    
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [bgView addSubview:saveBtn];
    saveBtn.frame = CGRectMake(0, bgView.height-44, bgView.width, 44);
    [saveBtn addTarget:self action:@selector(saveBtnClick) forControlEvents:UIControlEventTouchUpInside];
    saveBtn.backgroundColor = BtnBgColor;
    [saveBtn setTitle:@"保 存" forState:UIControlStateNormal];
    [saveBtn setTitleColor:BlackTextColor forState:UIControlStateNormal];
    saveBtn.titleLabel.font = FontWithSize(12);
}

- (void)saveBtnClick
{
    [self.changPriceTF becomeFirstResponder];
    
    if ([self.changPriceTF.text containsString:@"￥"])
    {
        self.changPriceTF.text = [self.changPriceTF.text stringByReplacingOccurrencesOfString:@"￥"withString:@""];
    }
    
    self.f_newPrice = [self.changPriceTF.text floatValue];
    if (self.f_newPrice < self.shopCostPrice)
    {
        [self ShowHUDWithMessage:@"销售价低于成本价"];
        return;
    }
    else if (self.f_newPrice > self.marketPrice)
    {
        [self ShowHUDWithMessage:@"销售价高于商品的市场价"];
        return;
    }
    
    
    
    if (self.moneyBlock)
    {
        self.moneyBlock(self.f_newPrice);
    }
    [self removeFromSuperview];
}

#pragma mark - 编辑结束的时候
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    if(textField == self.stockTF)
    {//自采商品的库存
        NSInteger editStock = [self.stockTF.text integerValue];
        if (editStock > self.stock)
        {
            [self ShowHUDWithMessage:@"不能高于当前库"];
            return NO;
        }
        
        return YES;
    }
    else if (textField == self.promotionPriceTF)
    {//自采商品的活动价
        float price = self.price;//零售价
        float castPrice = self.shopCostPrice;//成本价
        float sale = [self.promotionPriceTF.text floatValue];//用户修改的价格
        if (sale > price)
        {
            [self ShowHUDWithMessage:@"活动价不能高于零售价"];
            return NO;
        }
        
        if (sale < castPrice)
        {
            [self ShowHUDWithMessage:@"活动价不能低于成本价"];
            return NO;
        }
        return YES;
    }
    else {
        NSLog(@"编辑结束--%@",textField.text);
        if ([textField.text containsString:@"￥"])
        {
            textField.text = [textField.text stringByReplacingOccurrencesOfString:@"￥"withString:@""];
        }
        NSLog(@"编辑结束--%@",textField.text);
        if (NOEmptyStr(textField.text))
        {
            CGFloat nowNumber = [textField.text floatValue];
            if (nowNumber < self.shopCostPrice)
            {
                [self ShowHUDWithMessage:@"销售价低于成本价"];
            }
            else if (nowNumber > self.marketPrice)
            {
                [self ShowHUDWithMessage:@"销售价高于商品的市场价"];
            }else {
                self.changPriceTF.text = [NSString stringWithFormat:@"￥%0.2f",nowNumber];
                self.f_newPrice = nowNumber;
                self.earningsLabel.text = [NSString stringWithFormat:@"收益：¥%0.2f",(self.f_newPrice-self.shopCostPrice)];
            }
        }
        return YES;
    }
    
    
}

/*******************************************/

- (id)initPutShoppingWithModel:(MainModel *)model
{
    self = [super init];
    if (self)
    {
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        [self createPtuView:model];
    }
    
    return self;
}

- (void)createPtuView:(MainModel *)model
{
    UIView *bgView = [[UIView alloc] init];
    [self addSubview:bgView];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.center = self.center;
    bgView.bounds = CGRectMake(0, 0, 275, 460);
    bgView.layer.cornerRadius = 5;
    bgView.layer.masksToBounds = YES;
    
    UIImageView *closeIcon = [[UIImageView alloc] init];
    [bgView addSubview:closeIcon];
    closeIcon.frame = CGRectMake(bgView.width-10-16, 10, 16, 16);
    closeIcon.image = [UIImage imageNamed:@"closeIcon"];
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [bgView addSubview:closeBtn];
    closeBtn.frame = CGRectMake(bgView.width-40, 0, 40, 40);
    [closeBtn addTarget:self action:@selector(closeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    [bgView addSubview:titleLabel];
    titleLabel.frame = CGRectMake((bgView.width-90)/2, 20, 90, 14);
    titleLabel.font = FontWithSize(14);
    titleLabel.textColor = BlackTextColor;
    titleLabel.textAlignment = 1;
    titleLabel.text = @"确认上架信息";
    
    UIButton *mainBtnIcon = [UIButton buttonWithType:UIButtonTypeCustom];
    [bgView addSubview:mainBtnIcon];
    mainBtnIcon.frame = CGRectMake((bgView.width-150)/2, titleLabel.bottom+20, 150, 150);
    [mainBtnIcon addTarget:self action:@selector(selectPhotoBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    mainBtnIcon.backgroundColor = [UIColor lightGrayColor];
}

- (void)selectPhotoBtnClick:(UIButton *)sender
{
    BaseViewController *base = [BaseViewController new];
    [base takePhoto];
}

/************************/
/*
 *初始化活动弹框
 */
- (id)initActivityWithModel:(MainModel *)model isExChange:(NSInteger)isExChange
{
    self = [super init];
    if (self)
    {
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        [self createActivity:model isExChange:isExChange];
    }
    
    return self;
}

- (void)createActivity:(MainModel *)model isExChange:(NSInteger)isExChange
{
    UIView *bgView = [[UIView alloc] init];
    [self addSubview:bgView];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.center = self.center;
    bgView.bounds = CGRectMake(0, 0, 275, 255);
    bgView.layer.cornerRadius = 5;
    bgView.layer.masksToBounds = YES;
    
    UIImageView *closeIcon = [[UIImageView alloc] init];
    [bgView addSubview:closeIcon];
    closeIcon.frame = CGRectMake(bgView.width-10-16, 10, 16, 16);
    closeIcon.image = [UIImage imageNamed:@"closeIcon"];
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [bgView addSubview:closeBtn];
    closeBtn.frame = CGRectMake(bgView.width-40, 0, 40, 40);
    [closeBtn addTarget:self action:@selector(closeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    for (int i = 0; i < 2; i++)
    {
        UILabel *titleLabel = [[UILabel alloc] init];
        [bgView addSubview:titleLabel];
        titleLabel.frame = CGRectMake(20+(48)*(i%1), 71+(17+43)*(i/1), 48, 17);
        titleLabel.textAlignment = 0;
        titleLabel.textColor = RGBACOLOR(66, 66, 66, 1);
        titleLabel.font = FontWithSize(12);
        
        UIView *tfBgView = [[UIView alloc] init];
        [bgView addSubview:tfBgView];
        tfBgView.frame = CGRectMake(78+(180)*(i%1), 64+(30+30)*(i/1), 180, 30);
        
        UITextField *tf = [[UITextField alloc] init];
        [tfBgView addSubview:tf];
        tf.frame = CGRectMake(10, 0, 80, 30);
        tf.font = FontWithSize(11);
        tf.textColor = RGBACOLOR(66, 66, 66, 1);
        tf.textAlignment = 0;
        tf.delegate = self;
        
        UILabel *rightLabel = [[UILabel alloc] init];
        [tfBgView addSubview:rightLabel];
        rightLabel.frame = CGRectMake(tfBgView.width-110, 0, 100, 30);
        rightLabel.textAlignment = 2;
        rightLabel.font = FontWithSize(11);
        rightLabel.textColor = RGBACOLOR(153, 153, 153, 1);
        
        if (i == 0) {
            titleLabel.text = @"库   存";
            tf.text = [NSString stringWithFormat:@"%@",model.stockAmount];
            tf.keyboardType = UIKeyboardTypeNumberPad;
            tf.userInteractionEnabled = NO;
            self.stockTF = tf;
            self.stock = [model.stockAmount integerValue];
//            rightLabel.text = @"不能高于当前库存";
        }
        else
        {
            tfBgView.backgroundColor = RGBACOLOR(252, 252, 252, 1);
            tfBgView.layer.borderWidth = 1;
            tfBgView.layer.borderColor = RGBACOLOR(239, 239, 239, 1).CGColor;
            
            titleLabel.text = @"活动价格";
            
            if (isExChange == 1)
            {//活动商品
                self.price = [model.price floatValue];
                self.shopCostPrice = [model.shopCostPrice floatValue];
                float promotionPrice = [model.promotionPrice floatValue];
                tf.text = [NSString stringWithFormat:@"%0.2f",promotionPrice];
                rightLabel.text = [NSString stringWithFormat:@"零售价%0.2f",self.price];
            }else {
                //自采商品
                self.price = [model.price floatValue];
                self.shopCostPrice = [model.shopCostPrice floatValue];
                tf.text = [NSString stringWithFormat:@"%0.2f",self.price];
                rightLabel.text = [NSString stringWithFormat:@"正常售卖价%0.2f",self.price];
            }
            
            
            tf.keyboardType = UIKeyboardTypeDecimalPad;
            self.promotionPriceTF = tf;
            
        }
    }
    
    UILabel *areaLabel = [[UILabel alloc] init];
    [bgView addSubview:areaLabel];
    areaLabel.frame = CGRectMake(88, 159, bgView.width-88-10, 12);
    areaLabel.textColor = RGBACOLOR(153, 153, 153, 1);
    areaLabel.textAlignment = 0;
    areaLabel.font = FontWithSize(12);
    float price = [model.price floatValue];
    areaLabel.text = [NSString stringWithFormat:@"价格区间%@-%0.2f",@"0.1",price];
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [bgView addSubview:sureBtn];
    sureBtn.frame = CGRectMake(0, bgView.height-44, bgView.width, 44);
    [sureBtn addTarget:self action:@selector(sureUpLoadBtnClick) forControlEvents:UIControlEventTouchUpInside];
    sureBtn.backgroundColor = BtnBgColor;
    if (isExChange == 1)
    {
        //活动商品
        [sureBtn setTitle:@"保存" forState:UIControlStateNormal];
    }else {
        //自采商品
        [sureBtn setTitle:@"确认添加" forState:UIControlStateNormal];
    }
    
    [sureBtn setTitleColor:RGBACOLOR(66, 66, 66, 1) forState:UIControlStateNormal];
    sureBtn.titleLabel.font = FontWithSize(12);
}

- (void)sureUpLoadBtnClick
{
    if (IsEmptyStr(self.promotionPriceTF.text))
    {
        [self ShowHUDWithMessage:@"活动价不能为空"];
        return;
    }
    
    if (IsEmptyStr(self.stockTF.text))
    {
        [self ShowHUDWithMessage:@"库存不能为空"];
        return;
    }
    
    if (self.ActivityGoddsBlock) {
        self.ActivityGoddsBlock(self.promotionPriceTF.text, self.stockTF.text);
    }
}

#pragma mark - 展示弹出框
- (void)showAlertView
{
    UIWindow * window = [[[UIApplication sharedApplication]delegate]window];
    [window addSubview:self];
}

#pragma mark - 保证输入正确金额
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSLog(@"%@---%@",textField.text, string);
    BOOL isHaveDian = YES;
    if ([textField.text rangeOfString:@"."].location==NSNotFound)
    {
        isHaveDian=NO;
    }
    if ([string length]>0)
    {
        unichar single=[string characterAtIndex:0];//当前输入的字符
        if ((single >='0' && single<='9') || single=='.')//数据格式正确
        {
            //首字母不能为小数点
            if([textField.text length]==0)
            {
                if(single == '.')
                {
                    [textField.text stringByReplacingCharactersInRange:range withString:@""];
                    return NO;
                }
            }
            
            if([textField.text length]==1 && [textField.text isEqualToString:@"0"])
            {
                if(single != '.')
                {
                    [textField.text stringByReplacingCharactersInRange:range withString:@""];
                    return NO;
                }
            }
            
            if (single=='.')
            {
                if(!isHaveDian)//text中还没有小数点
                {
                    isHaveDian=YES;
                    return YES;
                }else
                {
                    [textField.text stringByReplacingCharactersInRange:range withString:@""];
                    return NO;
                }
            }
            else
            {
                if (isHaveDian)//存在小数点
                {
                    //判断小数点的位数
                    NSRange ran=[textField.text rangeOfString:@"."];
                    NSInteger tt=range.location-ran.location;
                    if (tt <= 2)
                    {
                        return YES;
                    }
                    else
                    {
                        return NO;
                    }
                }
                else
                {
                    return YES;
                }
            }
        }
        else
        {//输入的数据格式不正确
            [textField.text stringByReplacingCharactersInRange:range withString:@""];
            return NO;
        }
    }
    else
    {
        return YES;
    }
}


- (void)ShowHUDWithMessage:(NSString *)message
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    hud.bezelView.backgroundColor = UIColorFromHex(0xFF9D9D9D);
    hud.contentColor = [UIColor whiteColor];
    hud.label.text = NSLocalizedString(message, nil);
    hud.label.font = FontWithSize(12);
    [hud hideAnimated:YES afterDelay:HUDTime];
}

@end
